package p_103

import IDoSome
import java.util.*

class CacheControl constructor(val s: String) : IDoSome {

    private var maxAge: Date? = null
    private var sMaxAge: Date? = null
    private var mustReValidate = false
    private var noCache = false
    private var noRestore = false
    private var proxyReValidate = false
    private var publicCache = false
    private var privateCache = false


    override fun doSome() {
        val now = Date()
        val value = s.split(":")[1].trim()
        val components = value.split(",")

        for (component in components) {
            val com = component.trim().toLowerCase(Locale.US)

            when (com) {
                "private" -> privateCache = true
                "public" -> publicCache = true
                "no-cache" -> noCache = true
                "must-revalidate" -> mustReValidate = true
                "proxy-revalidate" -> proxyReValidate = true
                else -> {
                    when {
                        com.startsWith("max-age=") -> {
                            val secondsInTheFuture = com.substring(8).toInt()
                            maxAge = Date(now.time + secondsInTheFuture * 1000)
                        }
                        com.startsWith("s-maxage=") -> {
                            val secondsInTheFuture = com.substring(8).toInt()
                            sMaxAge = Date(now.time + secondsInTheFuture * 1000)
                        }
                    }
                }
            }

        }

    }

}